/*******************************地图相关************************************/
var geoChain;
var geoName;

var countryMap = {
    // 全国
    "china" : "000000"
};

var provinceMap = {
    // 直辖市、特别行政区当做省做特殊处理
    "北京": "110001", "天津": "120001", "上海": "310001", "重庆": "500001", "河北": "130000", "河南": "410000", "云南": "530000",
    "辽宁": "210000", "黑龙江": "230000", "湖南": "430000", "安徽": "340000", "山东": "370000", "新疆": "650000", "江苏": "320000",
    "浙江": "330000", "江西": "360000", "湖北": "420000", "广西": "450000", "甘肃": "620000", "山西": "140000", "内蒙古": "150000",
    "陕西": "610000", "吉林": "220000", "福建": "350000", "贵州": "520000", "广东": "440000", "青海": "630000", "西藏": "540000",
    "四川": "510000", "宁夏": "640000", "海南": "460000", "台湾": "710000", "香港": "810000", "澳门": "820000"
};

/**
 * 必须在form load时候就调用
 *
 */
function initMap(mapPreviousId, mapNextId) {
    var isDoubleClick;

    /* 地图选项、参数 */
    var optionMap = {
        title: {
            text: '地图',
        },
        tooltip: {
            trigger: 'item',
            formatter: '{b}'
        },
        series: [
            {
                name: '全国344个主要城市（县）地图',
                type: 'map',
                mapType: 'china',
                selectedMode: 'single',
                itemStyle: {
                    normal: {label: {show: true}},
                    emphasis: {label: {show: true}}
                },
                data: []
            }
        ]
    }
    geoChain = ['china'];    // 存储选中各级行政，默认选中全国
    geoName = '';

    var mapChart = echarts.init(document.getElementById('divMap'));
    var mapGeoData = echarts.util.mapData.params;

    /* 地图事件定义 */
    for (var city in cityMap) {
        // 自定义扩展图表类型
        mapGeoData.params[city] = {
            getGeoJson: (function (c) {
                var geoJsonName = cityMap[c];
                return function (callback) {
                    jQuery.getJSON(nonghuaBaseUrl + '/sites/all/libraries/echarts/geoJson/china-main-city/' + geoJsonName + '.json', callback);
                }
            })(city)
        }
    }

    var ecConfig = echarts.config;
    var zrEvent = zrender.tool.event;

    document.getElementById('divMap').onmousewheel = function (e) {
        var event = e || window.event;
        var isUpward = zrEvent.getDelta(event) > 0 ? true : false;

        // 向上滚动回退上级地图
        if (!isUpward) return;
        // 全国为最高级，不能再回退
        if (geoChain.length == 1) return;

        geoChain.pop();
        if (!!mapPreviousId) {
            reachPreviousEnd(mapPreviousId, mapNextId);
        }
        geoName = '';
        var mt = geoChain[geoChain.length - 1];

        optionMap.series[0].mapType = mt;
        mapChart.setOption(optionMap, true);
        zrEvent.stop(event);
        onMapsRollUp(e);
    };

    mapChart.on(ecConfig.EVENT.MAP_SELECTED, function (param) {
        isDoubleClick = false;
        window.setTimeout(cc, 500);
        function cc() {
            if (isDoubleClick != false) return;
            if (!param.selected[param.target]) {
                geoName = '';
            } else {
                geoName = param.target;
            }
            onMapsClick(param);
        }
    });

    mapChart.on(ecConfig.EVENT.DBLCLICK, function (param) {
        isDoubleClick = true;

        var mt = param.name;

        // 如果当前maptype为不可下钻，返回
        if (!provinceMap[mt] && !cityMap[mt]) return;

        optionMap.series[0].mapType = mt;
        geoChain.push(mt);
        if (!!mapNextId) {
            reachNextEnd(mapPreviousId, mapNextId, mt);
        }
        geoName = '';

        mapChart.setOption(optionMap, true);
        onMapsDblClick(param);
    });

    // 绑定前后按钮事件.模拟滚轮上滚和双击下钻
    if (!!mapPreviousId) {
        jQuery(mapPreviousId).click(function (event) {
            if (geoChain.length == 1) return;

            geoChain.pop();
            reachPreviousEnd(mapPreviousId, mapNextId);
            geoName = '';
            var mt = geoChain[geoChain.length - 1];

            optionMap.series[0].mapType = mt;
            mapChart.setOption(optionMap, true);
            onMapsRollUp(null);
        });
        if (geoChain.length == 1) {
            jQuery(mapPreviousId).attr({"disabled": "disabled"});
        }
    }

    if (!!mapNextId) {
        jQuery(mapNextId).click(function (event) {
            if (!geoName) {
                return;
            }
            var mt = geoName;
            if (!provinceMap[mt] && !cityMap[mt]) return;

            optionMap.series[0].mapType = mt;
            geoChain.push(mt);
            reachNextEnd(mapPreviousId, mapNextId, mt);
            geoName = '';

            mapChart.setOption(optionMap, true);
            onMapsDblClick(null);
        });
    }

    mapChart.setOption(optionMap, true);
}

function onMapsRollUp(e) {
    // Dummy function. Can be overloaded by individual pages.
}

function onMapsClick(param) {
    // Dummy function. Can be overloaded by individual pages.
}

function onMapsDblClick(param) {
    // Dummy function. Can be overloaded by individual pages.
}


function getGeoLevel(geoName)
{
    if (!geoName) {
        // 为空，默认选中全区域
        if (geoChain.indexOf('北京') >= 0 || geoChain.indexOf('天津') >= 0 || geoChain.indexOf('上海') >= 0 || geoChain.indexOf('重庆') >= 0) {
            return geoChain.length;
        } else {
            return geoChain.length - 1;
        }
    } else {
        // 直辖市的地理级别从2开始计算，因此+1
        if (geoName == '北京' || geoName == '天津' || geoName == '上海' || geoName == '重庆' ||
            geoChain.indexOf('北京') >= 0 || geoChain.indexOf('天津') >= 0 || geoChain.indexOf('上海') >= 0 || geoChain.indexOf('重庆') >= 0) {
            return geoChain.length + 1;
        } else {
            return geoChain.length;
        }
    }
}

function getNationalID(geoLevel, geoName)
{
    if (!geoName) {
        if (geoLevel == 0) {
            return countryMap['china'];
        }
        if (geoLevel == 1) {
            return provinceMap[geoChain[1]];
        }
        if (geoLevel == 2) {
            if (geoChain.length == 3) { // 非直辖市
                return cityMap[geoChain[2]];
            }
            if (geoChain.length == 2) { // 直辖市
                return provinceMap[geoChain[1]];
            }
        }
    } else {
        if (geoLevel <= 2) {
            if (!!provinceMap[geoName]) {
                return provinceMap[geoName];
            }
            if (!!cityMap[geoName]) {
                return cityMap[geoName];
            }
        }
        if (geoLevel == 3) {
            if (geoChain.length == 3) { // 非直辖市
                return cityMap[geoChain[2]];
            }
            if (geoChain.length == 2) { // 直辖市
                return provinceMap[geoChain[1]];
            }
        }
        return '';
    }
}

function reachPreviousEnd(mapPreviousId, mapNextId)
{
    if (geoChain.length == 1) {
        jQuery(mapPreviousId).attr({"disabled": "disabled"});
    }
    jQuery(mapNextId).removeAttr("disabled");
}

function reachNextEnd(mapPreviousId, mapNextId, mt)
{
    if (cityMap[mt] || mt == '北京' || mt == '天津' || mt == '上海' || mt == '重庆') {
        jQuery(mapNextId).attr({"disabled": "disabled"});
    }
    jQuery(mapPreviousId).removeAttr("disabled");
}